import { ElNotification as message } from "element-plus";
//1  import { Session } from "/@/utils/storage";
//import { getWsBaseURL } from "/@/utils/baseUrl"; //获取ws接口的方法,可自行换掉
// @ts-ignore
//import socket from "@/types/socket";
const connectURL = import.meta.env.VITE_CONNECT_URL;
const websocketSocket = {
  websocket: null,
  connectURL,
  // 开启标识
  socket_open: false,
  // 心跳timer
  hearbeat_timer: null,
  // 心跳发送频率
  hearbeat_interval: 5 * 1000,
  // 是否自动重连
  is_reonnect: true,
  // 重连次数
  reconnect_count: 5,
  // 已发起重连次数
  reconnect_current: 1,
  // 重连timer
  reconnect_timer: null,
  // 重连频率
  reconnect_interval: 10 * 1000,
  init: (receiveMessage) => {
    if (!("WebSocket" in window)) {
      message.warning("浏览器不支持WebSocket");
      return null;
    }
    //2 const token = Session.get("token");
    // if (!token) {
    //   message.warning("websocket认证失败");
    //   return null;
    // }
    const wsUrl = websocketSocket.connectURL;
    websocketSocket.websocket = new WebSocket(wsUrl);
    websocketSocket.websocket.onmessage = (e) => {
      if (receiveMessage) {
        let res = JSON.parse(e.data);
        receiveMessage(res);
      }
    };
    websocketSocket.websocket.onclose = (e) => {
      websocketSocket.socket_open = false;
      // 需要重新连接
      if (websocketSocket.is_reonnect) {
        websocketSocket.reconnect_timer = setTimeout(() => {
          // 超过重连次数
          if (websocketSocket.reconnect_current > websocketSocket.reconnect_count) {
            clearTimeout(websocketSocket.reconnect_timer);
            websocketSocket.is_reonnect = false;
            return;
          }
          // 记录重连次数
          websocketSocket.reconnect_current++;
          websocketSocket.reconnect();
        }, websocketSocket.reconnect_interval);
      }
    };
    // 连接成功
    websocketSocket.websocket.onopen = function () {
      websocketSocket.socket_open = true;
      websocketSocket.is_reonnect = true;
      // 开启心跳
      // websocket.heartbeat();
    };
    // 连接发生错误
    websocketSocket.websocket.onerror = function () {};
  },
  heartbeat: () => {
    websocketSocket.hearbeat_timer && clearInterval(websocketSocket.hearbeat_timer);

    websocketSocket.hearbeat_timer = setInterval(() => {
      let data = {
        token: "ping", //3. Session.get("token")
      };
      websocketSocket.send(data);
    }, websocketSocket.hearbeat_interval);
  },
  send: (data, callback) => {
    // 开启状态直接发送 websocket.websocket.readyState === websocket.websocket.OPEN
    if (websocketSocket.socket_open) {
      websocketSocket.websocket.send(JSON.stringify(data));
      // @ts-ignore
      callback && callback();
    } else {
      clearInterval(websocketSocket.hearbeat_timer);
      message({
        type: "warning",
        message: "socket链接已断开",
        duration: 1000,
      });
    }
  },
  close: () => {
    websocketSocket.is_reonnect = false;
    websocketSocket.websocket.close();
    websocketSocket.websocket = null;
  },
  /**
   * 重新连接
   */
  reconnect: () => {
    if (websocketSocket.websocket && !websocketSocket.is_reonnect) {
      websocketSocket.close();
    }
    websocketSocket.init(null);
  },
};
export default websocketSocket;
